package com.int_tech.report.controller;

import com.int_tech.car.dao.CarDao;
import com.int_tech.mession.dao.MessionDao;
import com.int_tech.mession.entity.Mession;
import com.int_tech.station.dao.StationDao;
import com.int_tech.station.entity.Station;
import com.int_tech.user.dao.UserDao;
import com.int_tech.user.entity.User;
import com.int_tech.utils.DateTool;
import com.int_tech.utils.poiOut.MessionPoi;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.smartcardio.Card;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liubch on 2017/10/11.
 */
@Controller
@RequestMapping("report")
public class ReportController {

    @Autowired
    private StationDao stationDao;

    @Autowired
    private CarDao carDao;

    @Autowired
    private MessionDao messionDao;

    @Autowired
    private UserDao userDao;


    /**
     * 工时报表
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/gongshi",method = RequestMethod.GET)
    public String gongshi(Model model, HttpServletRequest request) {

        /*int clientId = (int) request.getSession().getAttribute("uid");
        if(clientId == 1)
        {
            List<Station> stationList = stationDao.findAll();
            model.addAttribute("stationList",stationList);
        }
        else
        {
            List<Station> stationList = stationDao.findByClientId(clientId);
            model.addAttribute("stationList",stationList);
        }*/
        //通过用户登录id查询场站信息
//        int clientId = (int) request.getSession().getAttribute("uid");

        int clientId = -99;

        try
        {
            clientId = (int) request.getSession().getAttribute("uid");
        }
        catch (Exception e)
        {
            return "car/retry";
        }

        //新场站列表，需要过滤掉没有车辆的场站
        List<Station> stations = new ArrayList<>();

        if(clientId == 1)
        {
            List<Station> stationList = stationDao.findAll();
            for (Station station : stationList)
            {
                int count = carDao.countByStationIdAndIsDel(station.getId(),0);
                station.setCarCount(count);

                if(count > 0)
                {
                    stations.add(station);
                }
            }

            model.addAttribute("stationList",stations);
        }
        else
        {
            List<Station> stationList = stationDao.findByClientId(clientId);

            if(stationList == null || stationList.size() == 0)
            {
                User user = userDao.findById(clientId);

                Station station = stationDao.findById(Integer.parseInt(user.getStationId()));

                stationList = new ArrayList<>();

                stationList.add(station);
            }

            for (Station station : stationList)
            {
                int count = carDao.countByStationIdAndIsDel(station.getId(),0);
                station.setCarCount(count);

                if(count > 0)
                {
                    stations.add(station);
                }
            }
            model.addAttribute("stationList",stations);
        }
        return "/report/gongshi";
    }

    /**
     * 工时报表
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/report2",method = RequestMethod.GET)
    public String report2(Model model, HttpServletRequest request) {

        /*int clientId = (int) request.getSession().getAttribute("uid");
        if(clientId == 1)
        {
            List<Station> stationList = stationDao.findAll();
            model.addAttribute("stationList",stationList);
        }
        else
        {
            List<Station> stationList = stationDao.findByClientId(clientId);
            model.addAttribute("stationList",stationList);
        }*/
        //通过用户登录id查询场站信息
        //int clientId = (int) request.getSession().getAttribute("uid");
        int clientId = -99;

        try
        {
            clientId = (int) request.getSession().getAttribute("uid");
        }
        catch (Exception e)
        {
            return "car/retry";
        }

        //新场站列表，需要过滤掉没有车辆的场站
        List<Station> stations = new ArrayList<>();

        if(clientId == 1)
        {
            List<Station> stationList = stationDao.findAll();
            for (Station station : stationList)
            {
                int count = carDao.countByStationIdAndIsDel(station.getId(),0);
                station.setCarCount(count);
                if(count > 0)
                {
                    stations.add(station);
                }
            }
            model.addAttribute("stationList",stations);
        }
        else
        {
            List<Station> stationList = stationDao.findByClientId(clientId);

            if(stationList == null || stationList.size() == 0)
            {
                User user = userDao.findById(clientId);

                Station station = stationDao.findById(Integer.parseInt(user.getStationId()));

                stationList = new ArrayList<>();

                stationList.add(station);
            }

            for (Station station : stationList)
            {
                int count = carDao.countByStationIdAndIsDel(station.getId(),0);
                station.setCarCount(count);
                if(count > 0)
                {
                    stations.add(station);
                }
            }
            model.addAttribute("stationList",stations);
        }
        return "/report/report2";
    }


    /**
     * 工时报表
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/report3",method = RequestMethod.GET)
    public String report3(Model model, HttpServletRequest request) {

        /*int clientId = (int) request.getSession().getAttribute("uid");
        if(clientId == 1)
        {
            List<Station> stationList = stationDao.findAll();
            model.addAttribute("stationList",stationList);
        }
        else
        {
            List<Station> stationList = stationDao.findByClientId(clientId);
            model.addAttribute("stationList",stationList);
        }*/
        //通过用户登录id查询场站信息
        //int clientId = (int) request.getSession().getAttribute("uid");
        int clientId = -99;

        try
        {
            clientId = (int) request.getSession().getAttribute("uid");
        }
        catch (Exception e)
        {
            return "car/retry";
        }

        //新场站列表，需要过滤掉没有车辆的场站
        List<Station> stations = new ArrayList<>();

        if(clientId == 1)
        {
            List<Station> stationList = stationDao.findAll();
            for (Station station : stationList)
            {
                int count = carDao.countByStationIdAndIsDel(station.getId(),0);
                station.setCarCount(count);
                if(count > 0)
                {
                    stations.add(station);
                }
            }
            model.addAttribute("stationList",stations);
        }
        else
        {
            List<Station> stationList = stationDao.findByClientId(clientId);


            if(stationList == null || stationList.size() == 0)
            {
                User user = userDao.findById(clientId);

                Station station = stationDao.findById(Integer.parseInt(user.getStationId()));

                stationList = new ArrayList<>();

                stationList.add(station);
            }


            for (Station station : stationList)
            {
                int count = carDao.countByStationIdAndIsDel(station.getId(),0);
                station.setCarCount(count);
                if(count > 0)
                {
                    stations.add(station);
                }
            }
            model.addAttribute("stationList",stations);
        }
        return "/report/report3";
    }

    @RequestMapping(value = "/exportReport", method = RequestMethod.GET)
    public String exportReport(HttpServletRequest request, HttpServletResponse response, String carno, String cTime, String endTime) throws UnsupportedEncodingException {
        if(StringUtils.isEmpty(cTime))
        {
            cTime = DateTool.Date2String(new Date(),"yyyy-MM-dd 00:00:00");
        }
        else
        {
            cTime = cTime.split(" ")[0]+" 00:00:00";
        }

        if(StringUtils.isEmpty(endTime))
        {
            endTime = DateTool.Date2String(new Date(),"yyyy-MM-dd 23:59:59");
        }
        else
        {
            endTime = endTime.split(" ")[0]+" 23:59:59";
        }
        Date cdate = DateTool.string2Date(cTime,"yyyy-MM-dd HH:mm:ss");
        Date edate = DateTool.string2Date(endTime,"yyyy-MM-dd HH:mm:ss");
        List<Mession> messionList = messionDao.findByIsOverAndCreateTimeAfterAndCreateTimeBeforeAndYiChangAndCheckStatus(1,cdate,edate,0,3);
        List<Mession> outMessionList = new ArrayList<>();

        if(StringUtils.isEmpty(carno) || "全部车辆".equals(carno))
        {
            outMessionList = messionList;
        }
        else
        {
            for (Mession mession:messionList)
            {
                if(mession.toString().contains(carno))
                {
                    outMessionList.add(mession);
                }
            }
        }

        MessionPoi.messionPoi(outMessionList, response);
        JSONObject object = new JSONObject();

        object.put("status","true");

        return object.toString();
    }


}
